package com.wf.ew.apint.controller;

import com.alibaba.fastjson.JSON;
import com.wf.ew.apint.utils.CalcBox;
import com.wf.ew.common.BaseController;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.common.utils.wechart.WeChatPay;
import com.wf.ew.orderB.model.Bill;
import com.wf.ew.orderB.model.CarGps;
import com.wf.ew.orderB.service.BillService;
import com.wf.ew.orderB.service.BoxSearchService;
import com.wf.ew.orderB.service.CarGpsService;
import com.wf.ew.orderB.service.CustomerService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Damon
 * @create 2020-04-15 16:35
 */
@RestController
@RequestMapping("/apint/bill")
public class AllBillController extends BaseController {
    @Autowired
    private BillService billService;
    @Autowired
    private CarGpsService carGpsService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private BoxSearchService boxSearchService;


    /**
     * 查询列表
     */
    @RequestMapping(value = "/list", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String list(PageResult<T> page, Bill bill){
        if(Utils.isNotEmpty(bill)){
            if(Utils.isNotEmpty(bill.getBillOfLading())){
                return billService.allApiList(page, bill,getLoginUser());
            }
        }
       return getPageResult(page);
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/selectWkOrderOne", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String selectWkOrderOne(Bill bill){
        return JSONArray.fromObject(billService.getById(bill)).toString();
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/selectLonLat", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String selectLonLat(CarGps carGps){
        return JSONArray.fromObject(carGpsService.selectLonLat(carGps)).toString();
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/order/selectWkOrderFirst", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String selectWkOrderFirst(Bill bill){
        return JSONArray.fromObject(billService.selectWkOrderFirst(bill)).toString();
    }
    /**
     * 查询列表
     * 扫描客户二维码
     */
    @RequestMapping(value = "/order/selectOrderCus", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String selectOrderCus(Bill bill){
        return JSONArray.fromObject(billService.selectOrderCus(bill)).toString();
    }
    /**
     * 查询列表
     * 扫描客户二维码
     */
    @RequestMapping(value = "/order/selectShareOrders", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String selectShareOrders(Bill bill){
        return JSONArray.fromObject(billService.selectShareOrders(bill)).toString();
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/customerListAll", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String customerListAll(){
        return JSONArray.fromObject(customerService.customerListAll()).toString();
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/calcBoxes", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String calcBoxes(String ckName){
        CalcBox c = new CalcBox();
        return c.calc(ckName, boxSearchService);
    }
    /**
     * 查询列表
     */
    @RequestMapping(value = "/user/getOpenId", produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    public String getOpenId(String code, String encryptedData, String iv){
        WeChatPay wcp = new WeChatPay();
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> data = wcp.getAccess_tokenByCode(code);
        String result = WeChatPay.decryptData(encryptedData, data.get("session_key"), iv);
        //解析微信绑定的信息
        Map<String, Object> mapPhone = (Map<String, Object>) JSON.parse(result);
        JSONObject ob = new JSONObject();
        ob.put("purePhoneNumber", mapPhone.get("purePhoneNumber"));
        System.out.println("purePhoneNumber"+ mapPhone.get("purePhoneNumber"));
        return ob.toString();

    }
}
